$(function() {
	//获取购买商品id
	var localUrl = window.location.href;
	var localData = localUrl.split("=");
	var localText = decodeURI(localData[1]);
	//全局变量
	var totalMoney = 0;
	var tt = 0;
	var goodsIds = "";
	//提交订单
	$(".btn-go").click(function() {
		var lxr = $("#lxr").val();
		var lxrPhone = $("#lxrPhone").val();
		var lxrAddress = $("#lxrAddress").val();
		var flag = true;
		if (lxr == '') {
			flag = false;
			layer.tips('请输联系人姓名', '#lxr', {
				tipsMore: true,
			});
		}
		if (lxrPhone == '') {
			flag = false;
			layer.tips('请输入联系人电话', '#lxrPhone', {
				tipsMore: true,
			});
		}
		if (lxrAddress == '') {
			flag = false;
			layer.tips('请输入收货地址', '#lxrAddress', {
				tipsMore: true,
			});
		}
		if (!flag) {
			return;
		}
		var reg = /,$/gi;
		goodsIds = goodsIds.replace(reg, "");
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/order/createOrder",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			data: {
				"amount": tt,
				"address": lxrAddress,
				"contacts": lxr,
				"phone": lxrPhone,
				"goodsIds": goodsIds
			},
			success: function(data) {
				layer.msg(data.msg);
			}
		});
	})

	//点击商品名称，跳转到商品详情
	$(".J_Shop").on("click", ".toxq", function() {
		var goodsId = $(this).attr("data-id");
		if (goodsId == '') {
			layer.msg("数据异常");
		} else {
			var target = encodeURI("./xiangqing.html?id=" + goodsId);
			location.href = target;
		}
	});

	//获取购买商品信息
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/goods/getOrderGoods",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		data: {
			"ids": localText
		},
		success: function(data) {
			var obj = data.data;
			var node = $(".J_Shop");
			node.empty();
			var t = "";
			obj.forEach(function(goods) {
				goodsIds += goods.goods.id + ",";
				var count = goods.count;
				var tm = parseInt(goods.goods.price) * parseInt(count);
				totalMoney += tm;
				tt += tm;
				var specificationStr = "";
				var specification = goods.goods.specification;
				if (specification == 1) {
					specificationStr = "全网通版 6GB+64GB";
				} else if (specification == 2) {
					specificationStr = "全网通版 6GB+128GB";
				}
				var natureStr = "";
				var nature = goods.goods.nature;
				if (nature == 1) {
					natureStr = "黑亮色";
				} else if (nature == 2) {
					natureStr = "魅力蓝";
				} else if (nature == 3) {
					natureStr = "活力红";
				}
				t += '<tr class="item"><td class="s-title">' +
					'<a href="#" data-id="' + goods.goods.id + '" class="J_MakePoint toxq">' +
					'<img src="' + goods.goods.imgUrl + '" class="itempic">' +
					'<span class="title J_MakePoint">' + goods.goods.goodsName + '</span></a>' +
					'<div class="props"><span>' + specificationStr + '</span>' +
					'<span>' + natureStr + '</span></div><div>' +
					'<span style="color:gray;">卖家承诺72小时内发货</span>' +
					'</div></td><td class="s-price"><span class="price">' + goods.goods.price + '</span>' +
					'</td><td class="s-amount">' + count + '</td><td class="s-total">' +
					'<span class="price" id = "xj_price">' + tm + '</span></td>' +
					'</tr>';
			})
			t += '<tr class="blue-line" style="height: 2px;"><td colspan="4"></td>' +
				'</tr><tr class="other other-line"><td colspan="4"><ul class="dib-wrap">' +
				'<li class="dib extra-info"><div class="shoparea"><ul class="dib-wrap">' +
				'<li class="dib title">商品优惠：</li><li class="dib sel"></li>' +
				'<li class="dib fee"><span class="price">-0.00</span></li></ul></div>' +
				'<div class="shoppointarea"></div><div class="farearea"><ul class="dib-wrap J_farearea">' +
				'<li class="dib title">运送方式：</li><li class="dib sel">' +
				'<select class="J_Fare"><option value="0">免邮费</option><option value="1">快递 15.00元</option>' +
				'<option value="2">EMS 25.00元</option><option value="3">顺丰 25.00元</option>' +
				'</select></li><li class="dib fee"><span class="price">0.00</span>' +
				'</li></ul></div><div class="extra-area"><ul class="dib-wrap">' +
				'<li class="dib title">发货时间：</li><li class="dib content">卖家承诺订单在买家付款后，72小时内发货</li>' +
				'</ul></div><div class="servicearea" style="display: none"></div>' +
				'</li></ul></td></tr><tr class="shop-total blue-line"><td colspan="4">合计(含运费)：' +
				'<span class="price g_price" id="total_price">&yen;' + totalMoney + '</span></td></tr>';
			node.html(t);
		}
	});

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code != 'success') {
				window.location.href = './login.html';
			}
		}
	});

	//选择不同的运送方式，合计变化
	$(".J_Shop").on("change", ".J_Fare", function() {
		var way = $(this).val();
		var freight = 0;
		if (way == 1) {
			freight = 15;
		} else if (way == 2 || way == 3) {
			freight = 25;
		}
		var totalAmount = parseInt(totalMoney) + freight;
		var node = $("#total_price");
		tt = totalAmount;
		node.empty();
		node.html('&yen;' + totalAmount);
	})
})
